﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;

namespace WinMTR.NET
{
    public partial class TraceComponent : Component
    {
        public TraceComponent()
        {
            InitializeComponent();
        }

        public TraceComponent(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        public class TraceArgs
        {
            public IPAddress Address { get; private set; }
            public int PingTimeout { get; private set; }
            public int PingInterval { get; private set; }
            public bool ResolveNames { get; private set; }
            public int PingSize { get; private set; }


            public TraceArgs(IPAddress address, int pingTimeout, int pingInterval, bool resolveNames, int pingSize)
            {

                Address = address;
                PingTimeout = pingTimeout;
                PingInterval = pingInterval;
                ResolveNames = resolveNames;
                PingSize = pingSize;
            }

        }




        public delegate void TraceEventHandler(object sender, TraceEventArgs e);
        public event TraceEventHandler TraceEvent;

        public void OnTraceEvent(TraceEventType tet)
        {
            if (TraceEvent != null)
            {
                TraceEvent(this, new TraceEventArgs(tet));
            }
        }


        public bool IsBusy { get; set; }

        RouteWorker bw = null;
        public void Start(IPAddress address, int pingTimeout, int pingTimeFrame, bool resolveNames, int pingSize)
        {
            IsBusy = true;
            if (bw != null && bw.IsBusy)
            {
                return;
            }

            bw = new RouteWorker(address);
            TraceArgs tArgs = new TraceArgs(address, pingTimeout, pingTimeFrame, resolveNames, pingSize);
            
            bw.RunWorkerAsync(tArgs);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        }

        public CollectedData Data { get; private set; }
        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

            Data = bw.Data;
            OnTraceEvent(TraceEventType.PingRecv);

        }




        



        public void Stop()
        {

            if (bw != null && bw.IsBusy)
                bw.CancelAsync();
            IsBusy = false;
        }

        public void Refresh()
        {
            if (bw != null)
            {

                
            }

        }



        







       

        void w_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }




       






    }


    public class CollectedData
    {
        public List<CollectedDataItem> Items{ get;private set; }
        public CollectedData()
        {
            Items = new List<CollectedDataItem>();
        }


    }

    public class CollectedDataItem
    {
        public IPAddress Address { get; set; }
        public string Hostname { get; set; }
        public int TTL{ get; set; }

        public long LastRoundTrip { get; set; }
        public long AvgRoundTrip { get; set; }
        public long BestRoundTrip { get; set; }
        public int SentPings { get; set; }
        public int RecvPings { get; set; }
        public float Loss { get; set; }

    }

}
